{extend name="platform/base" /}
{block name="resources"/}
<style>
.text-active, .active>.text {
    display: block;
}
</style>
{/block}
 {block name="main"}
<div class="row padder-v">
	<div class="col-sm-9">
		<a href="javascript:;" onclick="addAttributeService()" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> 添加类型 </a>
	</div>
	<div class="col-sm-2">
		<input type="text" class="input-sm form-control" id="search_text"
			placeholder="请输入类型名称">
	</div>
	<div class="col-sm-1">
		<button class="btn btn-sm btn-default" type="button"
			onclick="LoadingInfo(1)">搜索</button>
	</div>
</div>
<section class="panel panel-default">
	<div class="table-responsive">
		<table class="table table-striped b-light text-sm">
			<colgroup>
				<col width="3%">
				<col width="20%">
				<col width="20%">
				<col width="15%">
				<col width="20%">
				<col width="25%">
			</colgroup>
			<thead>
				<tr>
					<th><input type="checkbox"></th>
					<th>类型名称</th>
					<th class="left">属性标签</th>
					<th class="left">排序</th>
					<th class="center">启用</th>
<!-- 					<th class="center">是否推荐</th> -->
					<th class="center">操作</th>
				</tr>
			</thead>
			<tbody id="list">
				<tr></tr>
			</tbody>
		</table>
	</div>
	{include file="platform/page" /}
</section>
{/block} {block name="script"}
<script type="text/javascript">	
function setAttributeField(id, name, e){
	
	if(name == 'is_use'){
		//alert(name);
		var value = $(e).attr("is_use");
		//alert(value);
	}else{
		var value = $(e).val();
	}
	$.ajax({
		type:"post",
		url:"{:__URL('PLATFORM_MAIN/goods/setattributefield')}",
		data:{
			'id' : id,
			'name' : name,
			'value' : value
			},
		async:true,
		success: function (data) {
			//alert(data);
			if(data['code'] > 0){
				if(name == 'is_use'){
					if(value == 1)
					{
						$("#isuse_"+id).html('<i class="fa fa-check text-success text-active"></i>');
						$("#isuse_"+id).attr("is_use", '0');
					}else{
						$("#isuse_"+id).html('<i class="fa fa-times text-danger text"></i>');
						$("#isuse_"+id).attr("is_use", '1');
					}
				}
				showMessage('success', data["message"]);
			}else{
				showMessage('error', data["message"]);
			}
			
		}
	}); 
	
	
	
}
/* 跳转到添加类型 */
function addAttributeService() {
		location.href = "{:__URL('PLATFORM_MAIN/goods/addattributeservice')}";
	}
$(function(){
	LoadingInfo(1);
});
	//加载数据
	function LoadingInfo(pageIndex) {
		var search_text = $("#search_text").val();
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/goods/attributelist')}",
			async : true,
			data : {
				"pageIndex" : pageIndex, "search_text" : search_text
			},
			success : function(data) {
				var html = '';
				$("#total_count_num").text(data["total_count"]);
				$("#page_count_num").text(data["page_count"]);
				$("#page_count").val(data["page_count"]);
				$("#pageNumber a").remove();
				if (data["data"].length > 0) {
					for (var i = 0; i < data["data"].length; i++) {
						html += '<tr>';
						html += '<td><input type="checkbox" name="sub" value="'+ data['data'][i]['spec_id']+'"></td>';
						html += '<td>'+ data["data"][i]["attr_name"] +'</td>';
						html += '<td class="left">' + data["data"][i]["value_str"] + '</td>';
						html += '<td class="center"><input class="form-control" type="text" id="sort" value="' + data["data"][i]["sort"] + '" onchange="setAttributeField('+data["data"][i]["attr_id"]+', \'sort\', this);"/></td>';
						if(data["data"][i]["is_use"]){
							html += '<td  class="center"><a href="javascript:;" class="is_use" id="isuse_'+data["data"][i]["attr_id"]+'" is_use="0" onclick="setAttributeField('+data["data"][i]["attr_id"]+', \'is_use\', this)"><i class="fa fa-check text-success text-active"></i></a></td>';
						}else{
							html += '<td  class="center"><a href="javascript:;" class="is_use" id="isuse_'+data["data"][i]["attr_id"]+'" is_use="1" onclick="setAttributeField('+data["data"][i]["attr_id"]+', \'is_use\', this)"><i class="fa fa-times text-danger text"></i></a></td>';
						}
						html += '<td class="center"><a href="'+  __URL('PLATFORM_MAIN/goods/updategoodsattribute?attr_id=' + data["data"][i]["attr_id"]) + '">修改</a>&nbsp;&nbsp;&nbsp;  <a href="javascript:delAttr(' + data["data"][i]["attr_id"] + ')">删除</a></td> ';
						html += '</tr>';
					}
				} else {
					html += '<tr class="center"><td colspan="6">暂无商品类型数据记录</td></tr>';
				}
				$("#list").html(html);
				var totalpage = $("#page_count").val();
				if (totalpage == 1) {
					changeClass("all");
				}
				var $html = pagenumShow(jumpNumber,totalpage,{$pageshow})
				$("#pageNumber").append($html);
			}
		});
	}
	/* 删除类型 */
	function delAttr(attr_id) {
		$( "#dialog" ).dialog({
			buttons: {
				"确定": function() {
					$(this).dialog('close');
					$.ajax({
						type : "post",
						url : "{:__URL('PLATFORM_MAIN/goods/deleteattr')}",
						data : {
							'attr_id' : attr_id
						},
						dataType : "json",
						success : function(data) {
							if (data['code'] > 0) {
	 							showMessage('success', data["message"],"{:__URL('PLATFORM_MAIN/goods/attributelist')}");
	 							
							}else if(data['code'] == 0){
	 							showMessage('error', data["message"]);
							}else if(data['code'] == -1){
								showMessage('error', '当前规格已经使用过，不能删除！');
							}
						}
					});
				},"取消,#e57373" : function(){
					$(this).dialog('close');
				}
			},
			contentText:"你确定删除吗？",
			title:"消息提醒"
		});
	}
</script>
{/block}
